package org.paybiz.core.payplatform.tenPay.util;

import java.security.MessageDigest;

/**
 * 类说明:temPayMD5加密
 * @author  lichunming
 * @version V1.0  创建时间：2009-12-20 上午11:45:47
 */
public class MD5Util {
	public MD5Util() {
	}


	private static String byteArrayToHexString(byte b[]) {
		StringBuffer resultSb = new StringBuffer();
		for (int i = 0; i < b.length; i++)
			resultSb.append(byteToHexString(b[i]));

		return resultSb.toString();
	}

	private static String byteToHexString(byte b) {
		int n = b;
		if (n < 0)
			n += 256;
		int d1 = n / 16;
		int d2 = n % 16;
		return hexDigits[d1] + hexDigits[d2];
	}

	public static String MD5Encode(String origin, String charsetname) {
		String resultString = null;
		try {
			resultString = new String(origin);
			MessageDigest md = MessageDigest.getInstance("MD5");
			if (charsetname == null || "".equals(charsetname))
				resultString = byteArrayToHexString(md.digest(resultString
						.getBytes()));
			else
				resultString = byteArrayToHexString(md.digest(resultString
						.getBytes(charsetname)));
		} catch (Exception exception) {
		}
		return resultString;
	}

	private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

	public static void main(String[] args) {
        System.out.println(MD5Encode("123456", "GB2312"));
        String str = "PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iR0IyMzEyIj8+Cjxyb290PjxvcF9jb2RlPjEw MTM8L29wX2NvZGU+PG9wX25hbWU+YmF0Y2hfZHJhdzwvb3BfbmFtZT48b3BfdXNlcj4xMjEwODQz NDAxPC9vcF91c2VyPjxvcF9wYXNzd2Q+Y3lobDU4NDEzMTQyMTwvb3BfcGFzc3dkPjxvcF90aW1l PjIwMTExMDE3MTIzNjQ3MzEyPC9vcF90aW1lPjxzcF9pZD4xMjEwODQzNDAxPC9zcF9pZD48cGFj a2FnZV9pZD4yMDExMTAxNzEyMzY0NzMxMjE8L3BhY2thZ2VfaWQ+PHRvdGFsX251bT4xPC90b3Rh bF9udW0+PHRvdGFsX2FtdD4xPC90b3RhbF9hbXQ+PGNsaWVudF9pcD4yMTguMTcuMTgxLjY5PC9j bGllbnRfaXA+PHJlY29yZF9zZXQ+PHJlY29yZD48c2VyaWFsPjE8L3NlcmlhbD48cmVjX2Jhbmth Y2M+NjIyNzAwNzIwMDEyMDg5Nzc5MDwvcmVjX2JhbmthY2M+PGJhbmtfdHlwZT4xMDAzPC9iYW5r X3R5cGU+PHJlY19uYW1lPs7EvrPBtTwvcmVjX25hbWU+PHBheV9hbXQ+MTwvcGF5X2FtdD48YWNj X3R5cGU+MTwvYWNjX3R5cGU+PGFyZWE+MjA8L2FyZWE+PGNpdHk+NzU1PC9jaXR5PjxzdWJiYW5r X25hbWU+PC9zdWJiYW5rX25hbWU+PGRlc2M+PC9kZXNjPjwvcmVjb3JkPjwvcmVjb3JkX3NldD48 L3Jvb3Q+";
        
        System.out.println(MD5Encode(str, "GB2312"));
    }
}
